﻿Imports System
Imports System.Collections.Specialized
Imports System.Configuration
Imports System.Diagnostics
Imports System.Globalization
Imports System.Transactions
Imports System.Workflow.Runtime

Namespace System.Workflow.Runtime.Hosting
    Public Class DefaultWorkflowCommitWorkBatchService
        Inherits WorkflowCommitWorkBatchService
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal parameters As NameValueCollection)
            If (parameters Is Nothing) Then
                Throw New ArgumentNullException("parameters", ExecutionStringManager.MissingParameters)
            End If
            If (parameters.Count > 0) Then
                Dim str As String
                For Each str In parameters.Keys
                    If (String.Compare("EnableRetries", str, StringComparison.OrdinalIgnoreCase) = 0) Then
                        Me._enableRetries = Boolean.Parse(parameters.Item(str))
                        Me._ignoreCommonEnableRetries = True
                    End If
                Next
            End If
        End Sub

        Protected Friend Overrides Sub CommitWorkBatch(ByVal commitWorkBatchCallback As CommitWorkBatchCallback)
            Dim retry As New DbRetry(Me._enableRetries)
            Dim retryCount As Short = 0
        Label_000E:
            If (Not Nothing Is Transaction.Current) Then
                retryCount = retry.MaxRetries
            End If
            Try 
                MyBase.CommitWorkBatch(commitWorkBatchCallback)
            Catch exception As Exception
                WorkflowTrace.Host.TraceEvent(TraceEventType.Error, 0, ("DefaultWorkflowCommitWorkBatchService caught exception from commitWorkBatchCallback: " & exception.ToString))
                If Not retry.TryDoRetry((retryCount)) Then
                    Throw
                End If
                WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, ("DefaultWorkflowCommitWorkBatchService retrying commitWorkBatchCallback (retry attempt " & retryCount.ToString(CultureInfo.InvariantCulture) & ")"))
                goto Label_000E
            End Try
        End Sub

        Protected Overrides Sub OnStopped()
            WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, "DefaultWorkflowCommitWorkBatchService: Stopping")
            MyBase.OnStopped
        End Sub

        Protected Friend Overrides Sub Start()
            WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, "DefaultWorkflowCommitWorkBatchService: Starting")
            If (Not Me._ignoreCommonEnableRetries AndAlso (Not MyBase.Runtime Is Nothing)) Then
                Dim commonParameters As NameValueConfigurationCollection = MyBase.Runtime.CommonParameters
                If (Not commonParameters Is Nothing) Then
                    Dim str As String
                    For Each str In commonParameters.AllKeys
                        If (String.Compare("EnableRetries", str, StringComparison.OrdinalIgnoreCase) = 0) Then
                            Me._enableRetries = Boolean.Parse(commonParameters.Item(str).Value)
                            Exit For
                        End If
                    Next
                End If
            End If
            MyBase.Start
        End Sub


        ' Properties
        Public Property EnableRetries As Boolean
            Get
                Return Me._enableRetries
            End Get
            Set(ByVal value As Boolean)
                Me._enableRetries = value
                Me._ignoreCommonEnableRetries = True
            End Set
        End Property


        ' Fields
        Private _enableRetries As Boolean
        Private _ignoreCommonEnableRetries As Boolean
    End Class
End Namespace

